"""
Case Type   : fullsql
Case Name   : track_stmt_details_size参数set异常输入校验,默认值为4096
Create At   : 2022/02/07
Owner       : zhao-pengcheng-g
Description :
    1.设置为OFF,OFF
    2.设置为小写off,off
    3.设置为小写l
    4.设置为大写L
    5.设置为取值范围以外的值
    6.设置为数字1,1
    7.设置分隔符为逗号以外的符号
    8.设置为Level1,Level2
    9.环境清理
Expect      :
    1.设设置成功,系统表不记录任何信息
    2.设置成功
    3.设置成功
    4.设置成功
    5.合理报错
    6.合理报错
    7.合理报错
    8.合理报错
    9.环境清理成功
History     :
"""

import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common


class Fullsql(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        text = '---Opengauss_Function_Fullsql_Case0027 start---'
        self.logger.info(text)
        self.userNode = Node('PrimaryDbUser')
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.para_name = 'track_stmt_details_size'

    def test_fullsql(self):
        text = '---step1:设置为最小值0,查看参数,expect:设置成功，查看参数结果为0---'
        self.logger.info(text)
        sql_cmd = f'''show track_stmt_details_size;
                      set track_stmt_details_size=0;
                      show track_stmt_details_size;'''
        sql_msg = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.para_default = sql_msg.splitlines()[2].strip()
        self.assertIn(self.constant.SET_SUCCESS_MSG and
                      '0', sql_msg, '执行失败:' + text)

        text = "---step2:设置为默认值4096,查看参数," \
               "expect:设置成功，查看参数结果为4096---"
        self.logger.info(text)
        sql_cmd = f'''set track_stmt_details_size=4096;
                    show track_stmt_details_size;'''
        sql_msg = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn(self.constant.SET_SUCCESS_MSG and
                      '4096', sql_msg, '执行失败:' + text)

        text = "---step3:设置为最大值100000000,查看参数," \
               "expect:设置成功，查看参数结果为4096---"
        self.logger.info(text)
        sql_cmd = f'''set track_stmt_details_size=100000000;
                    show track_stmt_details_size;'''
        sql_msg = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn(self.constant.SET_SUCCESS_MSG and
                      '100000000', sql_msg, '执行失败:' + text)

        text = "---step4:设置为-1,查看参数," \
               "expect:设置失败，合理报错---"
        self.logger.info(text)
        sql_cmd = f'''set track_stmt_details_size=-1;
                        show track_stmt_details_size;'''
        sql_msg = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn(self.constant.OUTSIDE_VALID_RANGE_MSG,
                      sql_msg, '执行失败:' + text)

        text = "---step5:设置为a,查看参数," \
               "expect:设置失败，合理报错---"
        self.logger.info(text)
        sql_cmd = f'''set track_stmt_details_size=a;
                    show track_stmt_details_size;'''
        sql_msg = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn(f"parameter \"{self.para_name}\" "
                      f"requires a numeric value" and self.para_default,
                      sql_msg, '执行失败:' + text)

        text = "---step6:设置为特殊字符&……%%￥,查看参数," \
               "expect:设置失败，合理报错---"
        self.logger.info(text)
        sql_cmd = f'''set track_stmt_details_size='!#$%@#%$!^';
                            show track_stmt_details_size;'''
        sql_msg = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn(f"parameter \"{self.para_name}\" "
                      f"requires a numeric value" and self.para_default,
                      sql_msg, '执行失败:' + text)

        text = "---step7:设置为1KB,查看参数," \
               "expect:设置失败，合理报错---"
        self.logger.info(text)
        sql_cmd = f'''set track_stmt_details_size='1KB';
                            show track_stmt_details_size;'''
        sql_msg = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn(f"parameter \"{self.para_name}\" "
                      f"requires a numeric value" and self.para_default,
                      sql_msg, '执行失败:' + text)

    def tearDown(self):
        self.logger.info(
            '---Opengauss_Function_Fullsql_Case0027 finish---')
